// #ifdef APP
import TIM from '@tencentcloud/chat/index.js';
import TIMUploadPlugin from 'tim-upload-plugin';
import TIMProfanityFilterPlugin from 'tim-profanity-filter-plugin';
import {
	onReady,
	onNotReady,
	onError,
	onNetWork,
	onMessageReceived,
	onMessageReadByPeer,
	onConversationList,
	onGroupList,
	onKickedOut,
	onFriendList,
	onApplication
} from '../store/im/onImScript.js'
import $store from '@/im/store/im';
// #endif
import sheep from '@/sheep';

export function IMConfig() {
	// #ifdef APP
	let options = {
		SDKAppID: 1600063970,
	};
	// 创建sdk实例
	const tim = TIM.create(options)

	// 设置 SDK 日志输出级别 测试时建议0 发布时4
	tim.setLogLevel(1)
	wx.$app = tim
	wx.TIM = TIM
	// 注册腾讯云即时通信 IM上传插件
	tim.registerPlugin({
		'tim-upload-plugin': TIMUploadPlugin
	});
	// 注册腾讯云即时通信 IM本地审核插件
	tim.registerPlugin({
		'tim-profanity-filter-plugin': TIMProfanityFilterPlugin
	});
	// im准备完成
	tim.on(TIM.EVENT.SDK_READY, onReady, this)
	// im尚未准备完成
	tim.on(TIM.EVENT.SDK_NOT_READY, onNotReady, this)
	// im报错了
	tim.on(TIM.EVENT.ERROR, onError, this)
	// 监听网络状态 
	tim.on(TIM.EVENT.NET_STATE_CHANGE, onNetWork, this);
	// 收到消息 (包括单聊、群聊、群提示、群系统)
	tim.on(TIM.EVENT.MESSAGE_RECEIVED, onMessageReceived, this)
	// 收到已读通知
	tim.on(TIM.EVENT.MESSAGE_READ_BY_PEER, onMessageReadByPeer, this);
	// 收到单聊会话更新
	tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, onConversationList, this);
	// 收到群聊会话更新
	tim.on(TIM.EVENT.GROUP_LIST_UPDATED, onGroupList, this);
	// 收到下线通知
	tim.on(TIM.EVENT.KICKED_OUT, onKickedOut, this);
	// 收到好友列表更新
	tim.on(TIM.EVENT.FRIEND_LIST_UPDATED, onFriendList, this);
	// 好友申请
	tim.on(TIM.EVENT.FRIEND_APPLICATION_LIST_UPDATED, onApplication, this);
	// #endif
}

export function unreadNum() {
	sheep.$store('app').setUnreadNum(sheep.$store('user').isLogin ? sheep.$store('user').userInfo.mobile : '')
}


export function cdn(url = '', cdnurl = '') {
	if (!url) return '';
	if (url.indexOf('http') === 0) {
		return url;
	}
	if (cdnurl === '') {
		cdnurl = 'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com';
	}
	return cdnurl + url;
};

export const udid = 'xyh'

export function getFindPath() {
	uni.request({
		url: 'https://sns-beta-huanglv.shisan-xi.work/api/share/getConfig',
		method: 'GET',
		data: {
			platform: 'xyh'
		},
		success(res) {
			$store().url_data = res.data.data
		}
	})
}